var constract = require('../../../utils/constract.js');
var app = getApp();

var collectId;

Page({

  data: {
    remind_list:[]
  },  

  onLoad: function (options) {
    var cardId = '';
    var collectId = '';
    var type = options.state;
    if(type == 0) {
      cardId = options.id;
    }
    if(type == 1){
      collectId = options.id;
    }
    this.setData({
      cardId: cardId,
      collectId: collectId,
      type: type
    })
  },

  onShow: function() {
    var that = this;
    var cardId = this.data.cardId;
    collectId = this.data.collectId;
    var type = this.data.type;
    app.Verification(function (token) {
      wx.request({
        url: constract.remindList,
        header: {token: token},
        data: { cardId: cardId, myCardId: collectId },
        success: function (res) {
          that.setData({
            remind_list: res.data.data
          })
        }
      })
    })
  },

  operate: function(e){
    var that = this;
    var remindid = e.currentTarget.dataset.remindid;
    wx.showActionSheet({
      itemList: ['编辑', '删除'],
      success: function (res) {
        if (res.tapIndex == 0) {
          if (that.data.cardId) {
            wx.navigateTo({
              url: '../add-notice/add-notice?remindid=' + remindid + '&cardId=' + that.data.cardId + '&type=' + that.data.type,
            })
          }
          if (that.data.collectId) {
            wx.navigateTo({
              url: '../add-notice/add-notice?remindid=' + remindid + '&cardId=' + that.data.collectId + '&type=' + that.data.type,
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: '确定删除备注信息？',
            success: function (res) {
              if (res.confirm) {
                  wx.request({
                  url: constract.deleteRemind,
                  data: { id: remindid },
                  success: function (res) {
                    if (res.data.code == 12000) {
                      var cardId = that.data.cardId;
                      app.Verification(function (token) {
                        wx.request({
                          url: constract.remindList,
                          data: { cardId: cardId, myCardId: collectId },
                          header: {token: token},
                          success: function (res) {
                            if(res.data.code == 12000) {
                              that.setData({
                                remind_list: res.data.data
                              })
                            }
                          }
                        })
                      })
                    }
                  }
                })
              } else if (res.cancel) {

              }
            }
          })
        }
      },
    })
  },

  noticeAdd:function() {
    var that = this;
    if (that.data.cardId) {
      wx.navigateTo({
        url: '../add-notice/add-notice?cardId=' + that.data.cardId + '&type=' + that.data.type
      })
    }
    if (that.data.collectId) {
      wx.navigateTo({
        url: '../add-notice/add-notice?cardId=' + that.data.collectId + '&type=' + that.data.type
      })
    }
  }
})